<?php

class NCore_GPCSPort extends NCore_CharDataPort
{
	private $globalArray;
	private $globalArrayKey;
    private $globalArrayAbbreviation;

	public function __construct($key)
	{
		parent::__construct($key);
		$this->globalArrayKey = $key;	
	}
	public function withGlobalArray(&$array, $abbr)
	{
		$this->globalArray = $array;

        if ($abbr != 'g' && $abbr != 'p' && $abbr != 'c' && $abbr != 's')
        {
            throw new Exception('The withGlobalArray() method only accepts the characters \'g\', \'p\', \'c\', or \'s\' as the second argument.');
        }

        $this->globalArrayAbbreviation = $abbr;

		return $this;
	}
	public function getGlobalArray()
	{
		return $this->globalArray;
	}
	public function withGlobalArrayKey($key)
	{
		$this->globalArrayKey = $key;
		return $this;
	}
	public function getGlobalArrayKey()
	{
		return $this->globalArrayKey;
	}
    public function isPresent()
    {
        // quick continue if NULLS are allowed
        if($this->getAllowNull())
            return true;

        $ga = $this->getGlobalArray();
        if(!isset($ga[$this->getGlobalArrayKey()]))
        {
            return false;
        }

        return true;
    }

    public function isGetPort()
    {
        return ('g' == $this->globalArrayAbbreviation);
    }

    public function isPostPort()
    {
        return ('p' == $this->globalArrayAbbreviation);
    }

    public function isCookiePort()
    {
        return ('c' == $this->globalArrayAbbreviation);
    }

    public function isSessionPort()
    {
        return ('s' == $this->globalArrayAbbreviation);
    }

	protected function initializeValue()
	{
		if(isset($this->globalArray[$this->globalArrayKey]))
		{
			$this->withValue($this->globalArray[$this->globalArrayKey]);
		}
		$this->toggleValueHasBeenSet();
	}
}

?>